﻿using DLMemberPointMS.Entity.POCOModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLMemberPointMS.Common;
using DLMemberPointMS.Entity.ViewModel;

namespace DLMemberPointMS.Business
{
    public class ShopsService : BaseService<Shops>
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="ContactName"></param>
        /// <param name="Address"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public OperateResult Shops(string Name, string ContactName, string Address, int page, int limit)
        {
            var query = PredicateExtensions.True<Shops>();
            if (!string.IsNullOrWhiteSpace(Name))
            {
                query = query.And(c => c.S_Name.Contains(Name));
            }
            if (!string.IsNullOrWhiteSpace(ContactName))
            {
                query = query.And(c => c.S_ContactName.Contains(ContactName));
            }
            if (!string.IsNullOrWhiteSpace(Address))
            {
                query = query.And(c => c.S_Address.Contains(Address));
            }
            //总条数
            var rowCount = 0;
            var pageData = GetList(page, limit, ref rowCount, query, c => c.S_ID, true).Select(c => new
            {
                S_ID = c.S_ID,
                S_Name = c.S_Name,
                S_Category = ((ShopTypeEnum)c.S_Category).ToString(),
                S_ContactName = c.S_ContactName,
                S_ContactTel = c.S_ContactTel,
                S_Address = c.S_Address,
                S_Remark = c.S_Remark,
                S_IsHasSetAdmin =(c.S_IsHasSetAdmin).ToString()=="true"?"已分配": "未分配",
                S_CreateTime = c.S_CreateTime
            }).ToList();
            OperateResult result = null;
            if (pageData != null)
            {
                result = new OperateResult(code.Success,"查询成功", pageData, rowCount);
            }
            else
            {
                result = new OperateResult(code.Error, "没有数据");
            }

            return result;
        }
        /// <summary>
        /// 添加店铺信息事件
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public OperateResult AddShops(ShopEditViewModel shop)
        {
            ////非空验证
            if (string.IsNullOrWhiteSpace(shop.S_Name))
            {
                return new OperateResult(code.Error, "店铺名称不能为空");
            }
            if (string.IsNullOrWhiteSpace(shop.S_ContactName))
            {
                return new OperateResult(code.Error, "联系人不能为空");
            }
            if (string.IsNullOrWhiteSpace(shop.S_ContactTel))
            {
                return new OperateResult(code.Error, "联系人电话不能为空");
            }
            if (string.IsNullOrWhiteSpace(shop.S_Address))
            {
                return new OperateResult(code.Error, "店铺地址不能为空");
            }
            Shops shops = new Shops();

            shops.S_Name = shop.S_Name;
            shops.S_ContactName = shop.S_ContactName;
            shops.S_Category = shop.S_Category;
            shops.S_ContactTel = shop.S_ContactTel;
            shops.S_Address = shop.S_Address;
            shops.S_Remark = shop.S_Remark;
            shops.S_IsHasSetAdmin = shop.S_IsHasSetAdmin;
            shops.S_CreateTime = shop.S_CreateTime;

            if (Add(shops))
            {
                return new OperateResult(code.Success, "店铺添加成功");
            }
            else
            {
                return new OperateResult(code.Success, "店铺添加失败");
            }

        }
        /// <summary>
        /// 删除店铺信息
        /// </summary>
        /// <param name="S_ID"></param>
        /// <returns></returns>
        public OperateResult DeleShops(int S_ID)
        {
            if (Find(e => e.S_ID == S_ID) == null)
            {
                return new OperateResult(code.Error, "店铺信息不存在");
            }
            else
            {
                var data = Find(e => e.S_ID == S_ID);
                
                if (data.S_IsHasSetAdmin == true)
                {
                  
                      
                    return new OperateResult(code.Success, "店铺存在管理员，且不可删除");



                }
                else if (Delete(data))
                {
                    return new OperateResult(code.Success, "删除店铺信息成功");
                }
                else
                {
                    return new OperateResult(code.Error, "删除店铺信息失败");
                }

            }
        }

        public OperateResult UpdeteShops(ShopEditViewModel shop)
        {
            if (Find(e => e.S_ID == shop.S_ID) == null)
            {
                return new OperateResult(code.Error, "店铺信息不存在");
            }
            else
            {
                ////非空验证
                if (string.IsNullOrWhiteSpace(shop.S_Name))
                {
                    return new OperateResult(code.Error, "店铺名称不能为空");
                }
                if (string.IsNullOrWhiteSpace(shop.S_ContactName))
                {
                    return new OperateResult(code.Error, "联系人不能为空");
                }
                if (string.IsNullOrWhiteSpace(shop.S_ContactTel))
                {
                    return new OperateResult(code.Error, "联系人电话不能为空");
                }
                if (string.IsNullOrWhiteSpace(shop.S_Address))
                {
                    return new OperateResult(code.Error, "店铺地址不能为空");
                }
                var data = Find(e => e.S_ID == shop.S_ID);
                data.S_Name = shop.S_Name;
                data.S_ContactName = shop.S_ContactName;
                data.S_Category = shop.S_Category;
                data.S_ContactTel = shop.S_ContactTel;
                data.S_CreateTime = DateTime.Now;
                data.S_Address = shop.S_Address;
                data.S_Remark = shop.S_Remark;
                data.S_IsHasSetAdmin = false;
                if (Update(data))
                {
                    return new OperateResult(code.Success, "店铺修改成功");
                }
                else
                {
                    return new OperateResult(code.Success, "店铺修改失败");
                }
            }
        }
        public OperateResult Assign(int id)
        {
            if (Find(E => E.S_ID == id) == null)
            {
                return new OperateResult(code.Error, "店铺信息不存在");
            }
            else
            {
                var data = Find(E => E.S_ID == id);
                //判断店铺是否已经存在管理员
                if (data.S_IsHasSetAdmin == false)
                {
                    return new OperateResult(code.Success, "", data);
                }
                else
                {
                    return new OperateResult(code.Error,"店铺已存在管理员");
                }
            }
        }
        /// <summary>
        /// 分配管理员
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="S_ID"></param>
        /// <returns></returns>
        public OperateResult ShopsAssign(string Name, int S_ID)
        {
            BaseService<Users> Users = new BaseService<Users>();
            var usermodel = Users.Find(e => e.U_LoginName == Name);
            if (usermodel == null)
            {
                var user = new Users();
                user.S_ID = S_ID;
                user.U_LoginName = Name;
                user.U_Password = "123456";
                user.U_Role = 1;
                user.U_CanDelete = false;
                user.U_RealName = Name;
                var userAdd = Users.Add(user);
                var data = Find(e => e.S_ID == S_ID);
                data.S_IsHasSetAdmin = true;
                if (Update(data) && userAdd)
                {
                    return new OperateResult(code.Success, "添加成功");
                }
                else
                {
                    return new OperateResult(code.Success, "添加失败");
                }
            }
            else
            {
                return new OperateResult(code.Error, "用户名已存在");
            }
        }



    }
}
